public class test3 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        int sum;
        public boolean hasPathSum(TreeNode root, int targetSum) {
            sum = 0;
            return isValid(root,targetSum,0);
        }
        private boolean isValid(TreeNode root,int targetSum,int sum){
            if(root==null){
                return false;
            }
            sum += root.val;
            if(root.right==null && root.left==null && sum==targetSum){
                return true;
            }
            boolean b1 = isValid(root.left,targetSum,sum);
            boolean b2 = isValid(root.right,targetSum,sum);
            return b1 || b2;
        }
    }
}
